﻿using BySoft.Server.Business.UserCode;
using BySoft.Server.Common;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.BOM
{
    public partial class BOMView : AuthBasePage
    {
        public DataTable BOMMaterialDT
        {
            set { Session["BOMMaterialDT"] = value; }
            get
            {
                if (Session["BOMMaterialDT"] == null)
                {
                    return CreateDt();
                }
                else
                {
                    return (DataTable)Session["BOMMaterialDT"];
                }
            }
        }
        public DataTable CreateDt()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("BOMDetailID");
            dt.Columns.Add("IsDirect");//是否直接材料
            dt.Columns.Add("IsDirectName");
            dt.Columns.Add("MaterialID");//原料编号
            dt.Columns.Add("MaterialName");//原料名称
            dt.Columns.Add("Specifications");//规格
            dt.Columns.Add("Unit");//单位
            dt.Columns.Add("Long");//长
            dt.Columns.Add("Width");//宽
            dt.Columns.Add("Height");//高
            dt.Columns.Add("OutSide");//外径
            dt.Columns.Add("InSide");//内径
            dt.Columns.Add("MMNumber");//单件毫米数
            dt.Columns.Add("SingleWeight");//单件重量
            dt.Columns.Add("SingleNum");//单件数量
            dt.Columns.Add("IsNew");
            dt.Columns.Add("IsDel");
            return dt;
        }

        public DataTable BOMKnifeDT
        {
            set { Session["BOMKnifeDT"] = value; }
            get
            {
                if (Session["BOMKnifeDT"] == null)
                {
                    return CreateKnifeDt();
                }
                else
                {
                    return (DataTable)Session["BOMKnifeDT"];
                }
            }
        }
        public DataTable CreateKnifeDt()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("BOMKnifeID");
            dt.Columns.Add("KnifeID");//刀具编号
            dt.Columns.Add("KnifeName");//刀具名称
            dt.Columns.Add("Specifications");//规格
            dt.Columns.Add("Nums");//数量
            dt.Columns.Add("KnifeStatus");//刀具状态
            dt.Columns.Add("IsNew");
            dt.Columns.Add("IsDel");
            return dt;
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BOMKnifeDT = null;
                BOMMaterialDT = null;
                InitData();
            }
            txtProductID.Attributes.Add("readonly", "true");
            txtProductName.Attributes.Add("readonly", "true");
            txtUnit.Attributes.Add("readonly", "true");
            UIHelper.RunScript(this.Page, "iframeAutoFit();");
        }

        private void InitData()
        {
            string id = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("id"));
            if (!string.IsNullOrEmpty(id))
            {
                DataTable dt = BsBOMBusiness.GetBOMDetailByID(id);
                if (dt != null && dt.Rows.Count > 0)
                {
                    DataRow d = dt.Rows[0];
                    lblBOMID.Text = CommonMethod.GetString(d["BOMID"]);
                    lblBillDate.Text = string.Format("{0:yyyy-MM-dd}", d["BillDate"]);
                    txtProductID.Text = CommonMethod.GetString(d["ProductID"]);
                    hdWorkOrderNo.Value = CommonMethod.GetString(d["WorkOrderNo"]);
                    txtProductName.Text = CommonMethod.GetString(d["ProductName"]);
                    txtUnit.Text = CommonMethod.GetString(d["Unit"]);
                    lblRemark.Text = CommonMethod.GetString(d["Remark"]);

                    DataTable dtemp = BOMMaterialDT.Copy();
                    foreach (DataRow dr in dt.Rows)
                    {
                        DataRow drow = dtemp.NewRow();
                        drow["BOMDetailID"] = CommonMethod.GetString(dr["BOMDetailID"]);
                        drow["MaterialID"] = CommonMethod.GetString(dr["MaterialID"]);
                        drow["MaterialName"] = CommonMethod.GetString(dr["MaterialName"]);
                        drow["Specifications"] = CommonMethod.GetString(dr["Specifications"]);
                        drow["Unit"] = CommonMethod.GetString(dr["Unit"]);
                        drow["IsDirect"] = CommonMethod.GetString(dr["IsDirect"]);
                        drow["IsDirectName"] = CommonMethod.GetString(dr["IsDirect"]) == "1" ? "是" : "否";
                        drow["Long"] = CommonMethod.GetString(dr["Long"]);
                        drow["Width"] = CommonMethod.GetString(dr["Width"]);
                        drow["Height"] = CommonMethod.GetString(dr["Height"]);
                        drow["OutSide"] = CommonMethod.GetString(dr["OutSide"]);
                        drow["InSide"] = CommonMethod.GetString(dr["InSide"]);
                        drow["MMNumber"] = CommonMethod.GetString(dr["MMNumber"]);
                        drow["SingleWeight"] = CommonMethod.GetString(dr["SingleWeight"]);
                        drow["SingleNum"] = CommonMethod.GetString(dr["SingleNum"]);
                        drow["IsDel"] = "-1";
                        drow["IsNew"] = "-1";
                        dtemp.Rows.Add(drow);
                    }
                    BOMMaterialDT = dtemp.Copy();
                }
                DataTable dtKnife = BsBOMBusiness.GetBOMKnifeByID(id);
                if (dtKnife != null && dtKnife.Rows.Count > 0)
                {
                    DataTable d = BOMKnifeDT.Copy();
                    foreach (DataRow dr in dtKnife.Rows)
                    {
                        DataRow drow = d.NewRow();
                        drow["BOMKnifeID"] = CommonMethod.GetString(dr["BOMKnifeID"]);
                        drow["KnifeID"] = CommonMethod.GetString(dr["KnifeID"]);
                        drow["KnifeName"] = CommonMethod.GetString(dr["KnifeName"]);
                        drow["Specifications"] = CommonMethod.GetString(dr["Specifications"]);
                        drow["Nums"] = CommonMethod.GetString(dr["Nums"]);
                        drow["KnifeStatus"] = CommonMethod.GetString(dr["KnifeStatusName"]);
                        drow["IsDel"] = "-1";
                        drow["IsNew"] = "-1";
                        d.Rows.Add(drow);
                    }
                    BOMKnifeDT = d.Copy();
                }
            }
            BindMaterial();
            BindKnife();
        }

        private void BindMaterial()
        {
            DataView dv = new DataView(BOMMaterialDT);
            dv.RowFilter = " IsDel <> '1' ";
            DataTable dt = dv.ToTable();
            dt.Columns.Add("OrderNum");
            if (dt != null && dt.Rows.Count > 0)
            {
                int index = 1;
                foreach (DataRow dr in dt.Rows)
                {
                    dr["OrderNum"] = index;
                    index++;
                }
                grdResult.DataSource = dt;
                grdResult.DataBind();
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dt);
            }
            UIHelper.RunScript(this.UpdatePanel4, "iframeAutoFit();");
        }

        private void BindKnife()
        {
            DataView dv = new DataView(BOMKnifeDT);
            dv.RowFilter = " IsDel <> '1' ";
            DataTable dt = dv.ToTable();
            dt.Columns.Add("OrderNum");
            if (dt != null && dt.Rows.Count > 0)
            {
                int index = 1;
                foreach (DataRow dr in dt.Rows)
                {
                    dr["OrderNum"] = index;
                    index++;
                }
                grdKnife.DataSource = dt;
                grdKnife.DataBind();
            }
            else
            {
                UIHelper.BindEmptyGridView(grdKnife, dt);
            }
            UIHelper.RunScript(this.UpdatePanel2, "iframeAutoFit();");
        }
    }
}